let { ordersModel } = require("./models/ordersModel.js");
let { outOrdersModel } = require("./models/outOrdersModel.js");
let mongoose = require('mongoose')
// 新增
module.exports.addOrders = async function (sendData) {
  return await ordersModel.create(sendData);
};

// 通过订单id获取该订单的所有有关信息
module.exports.getOrdersByOrdersId = async function (sendData) {
  // let sendData = { _id }
  console.log(444,sendData);
  return await ordersModel.find(sendData).populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });
};

// 获取订单
module.exports.getOrders = async function () {
  return await ordersModel.find().populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });
};

// 搜索(管理界面，所有订单搜索)
module.exports.searchSome = async function (sendData) {
  console.log(3333,sendData);
  let { searchValue } = sendData;
  let searchSomeData = await ordersModel.find({
    // 根据订单流水号模糊查询
    flowNum: { $regex: searchValue, $options: "$i" },
  }).populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });

  return searchSomeData;
};

// // 销售界面
// // 通过销售id获取
module.exports.getOrdersBySaleMenId = async function (sendData) {
  console.log(sendData);
  return await ordersModel.find({
    saleMenId: sendData.saleMenId,
  }).populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });
};
// // 销售界面 搜索
module.exports.searchSomeById = async function (sendData) {
  let searchSomeData = await ordersModel.find({
    // 订单流水和销售的id同时满足
    saleMenId: sendData.saleMenId,
    flowNum: { $regex: sendData.searchValue, $options: "$i" },
  }).populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });
  return searchSomeData;
};


// 根据 订单 id 删除
module.exports.delOneOrders = async function (sendData) {
  // console.log(3333,sendData);
  return await ordersModel.deleteOne(sendData);
};


// 修改    

module.exports.oneModifyOrders = async function (sendData) {

  // 修改电话 和 姓名
  // console.log(666,sendData);
  let { _id,ordersName,ordersPhone,outOrdersState } = sendData;
  // console.log('修改',{ _id }, { ordersName,ordersPhone,outOrdersState});
  return await ordersModel.updateOne({ _id }, { ordersName,ordersPhone,outOrdersState});
  // console.log(222,sendData);  //{ n: 1, nModified: 1, ok: 1 }
}
// 管理员 修改 审核状态    

module.exports.passoutState = async function (sendData) {

  // 修改电话 和 姓名
  // console.log(666,sendData);
  let { _id,outOrdersState } = sendData;
  // console.log('修改',{ _id }, { ordersName,ordersPhone,outOrdersState});
  return await ordersModel.updateOne({ _id }, { outOrdersState});
  // console.log(222,sendData);  //{ n: 1, nModified: 1, ok: 1 }
}


// 通过状态获取
module.exports.getOrdersByState = async function (sendData) {
  const ObjectId = mongoose.Types.ObjectId;
  return await ordersModel.find({
    'saleMenId':new ObjectId(sendData.saleMenId),
    'state': Number(sendData.state)
  }).populate({
    // 关联产品id，客户id，销售id
    path: "productsId saleMenId customersId",
    populate:{
        path:'classesId'
    }
  });
  
};


// 新增 退保
module.exports.applyOutOrders = async function (sendData) {

  console.log('退保' ,sendData);
  return await outOrdersModel.create(sendData);
};


// // 通过销售id获取
module.exports.getOutOrdersBySaleMenId = async function (sendData) {
  // console.log(sendData);
  return await outOrdersModel.find({
    saleMenId: sendData.saleMenId,
  }).populate({
    // 关联产品id，客户id，销售id
    path: "oneOrdersId saleMenId ",
    populate:{
        path:'customersId productsId',
        populate:{
          path:'classesId',
          
      }
    }
  });
};
// // 获取所有 退货
module.exports.getOutOrders = async function (sendData) {
  // console.log(sendData);
  return await outOrdersModel.find({}).populate({
    // 关联产品id，客户id，销售id
    path: "oneOrdersId saleMenId ",
    populate:{
        path:'customersId productsId',
        populate:{
          path:'classesId',
          
      }
    }
  });
};

// 修改    

module.exports.modifyOutOrdersState = async function (sendData) {
  let {_id,outOrdersState} = sendData;
  
  let getData = await outOrdersModel.updateOne({_id},{outOrdersState});
  // console.log(222,getData);  //{ n: 1, nModified: 1, ok: 1 }

  return getData

};
